
# By default, this firmware is built with 288/32 flash/ram device configuration
# To build it with 256/64 configuration, run the following:
# make CFLAGS_EXTRA="-Wl,--defsym -Wl,RAM_64K=1"

ROOT ?= $(realpath $(PWD)/../../..)
PREFIX ?= C:\MounRiver\MRS_Community\toolchain\RISC-V Embedded GCC12\bin\riscv-none-elf
#PREFIX ?= docker run --platform linux/amd64 --rm -it -v$(ROOT):$(ROOT) -w $(PWD) mdashnet/riscv riscv-none-elf
CFLAGS = -W -Wall -Wextra -Wundef -Wshadow -Wdouble-promotion -Wno-format  
CFLAGS += -Wformat-truncation -Wconversion -Wno-sign-conversion
CFLAGS += -ffunction-sections -fdata-sections -fno-common
CFLAGS += -DSYSCLK_FREQ_144MHz_HSE -I. -Ivendor -g3 -Os
CFLAGS += -march=rv32imafc -mabi=ilp32
CFLAGS += -IPeripheral/inc 
CFLAGS += -INetLib 
CFLAGS += -ICore 
CFLAGS += -IDebug 
CFLAGS += -IUser
LDFLAGS = -T Ld/Link.ld -nostartfiles --specs=nano.specs --specs=nosys.specs -Wl,--gc-sections 
SOURCES += User/system_ch32v30x.c 
SOURCES += User/main.c 
SOURCES += User/ch32v30x_it.c 
SOURCES += User/hal.c 
SOURCES += User/mongoose.c 
SOURCES += User/net.c 
SOURCES += User/packed_fs.c 
SOURCES += User/hal.h 
SOURCES += User/mongoose_config.h 
SOURCES += NetLib/libwchnet.a 
SOURCES += NetLib/eth_driver_10M.c 
SOURCES += Peripheral/inc/ch32v30x.h 
SOURCES += Peripheral/src/ch32v30x_adc.c 
SOURCES += Peripheral/src/ch32v30x_bkp.c 
SOURCES += Peripheral/src/ch32v30x_can.c 
SOURCES += Peripheral/src/ch32v30x_crc.c 
SOURCES += Peripheral/src/ch32v30x_dac.c 
SOURCES += Peripheral/src/ch32v30x_dbgmcu.c 
SOURCES += Peripheral/src/ch32v30x_dma.c 
SOURCES += Peripheral/src/ch32v30x_dvp.c 
SOURCES += Peripheral/src/ch32v30x_eth.c 
SOURCES += Peripheral/src/ch32v30x_exti.c 
SOURCES += Peripheral/src/ch32v30x_flash.c 
SOURCES += Peripheral/src/ch32v30x_fsmc.c 
SOURCES += Peripheral/src/ch32v30x_gpio.c 
SOURCES += Peripheral/src/ch32v30x_i2c.c 
SOURCES += Peripheral/src/ch32v30x_iwdg.c 
SOURCES += Peripheral/src/ch32v30x_misc.c 
SOURCES += Peripheral/src/ch32v30x_opa.c 
SOURCES += Peripheral/src/ch32v30x_pwr.c 
SOURCES += Peripheral/src/ch32v30x_rcc.c 
SOURCES += Peripheral/src/ch32v30x_rng.c 
SOURCES += Peripheral/src/ch32v30x_rtc.c 
SOURCES += Peripheral/src/ch32v30x_sdio.c 
SOURCES += Peripheral/src/ch32v30x_spi.c 
SOURCES += Peripheral/src/ch32v30x_tim.c 
SOURCES += Peripheral/src/ch32v30x_usart.c 
SOURCES += Peripheral/src/ch32v30x_wwdg.c 
SOURCES += Debug/debug.c 
SOURCES += Core/core_riscv.c 
SOURCES += Startup/startup_ch32v30x_D8C.S 

CFLAGS += -DHTTP_URL=\"http://192.168.21.223:80/\" -DHTTPS_URL=\"https://192.168.21.223:443/\"
CFLAGS += -DWCHNETTIMERPERIOD=0
ifeq ($(OS),Windows_NT)
  RM = cmd /C del /Q /F /S
else
  RM = rm -rf
endif

all: firmware.bin

firmware.bin: firmware.elf
	$(PREFIX)-objcopy -O binary $< $@
	$(PREFIX)-size --format=berkeley $<

firmware.elf: $(SOURCES) Makefile
	$(PREFIX)-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) $(CFLAGS_EXTRA) -o $@

flash: firmware.bin
	C:\WCH\WCHISPTool flash $<
#	@echo; echo "IMPORTANT: configure device to 96k RAM / 224k Flash"

zip: firmware.bin
	zip -r ch32v307.modbus.firmware.`date +%Y-%m-%d`.zip  firmware.*

clean:
	$(RM) firmware.* *.zip

upload:firmware.bin
	C:/MounRiver/MRS_Community/toolchain/OpenOCD/bin/openocd.exe  -f C:/MounRiver/MRS_Community/toolchain/OpenOCD/bin/wch-riscv.cfg -c "reset_config trst_only combined" -c "program firmware.elf verify reset exit"
update:
	C:/MounRiver/MRS_Community/toolchain/OpenOCD/bin/openocd.exe -f C:/MounRiver/MRS_Community/toolchain/OpenOCD/bin/wch-riscv.cfg -c init -c halt -c "program firmware.bin verify reset exit"

reset:
	C:/MounRiver/MRS_Community/toolchain/OpenOCD/bin/openocd.exe -f C:/MounRiver/MRS_Community/toolchain/OpenOCD/bin/wch-riscv.cfg -c init -c halt -reset -c shutdown

debug-start:
	C:/MounRiver/MRS_Community/toolchain/OpenOCD/bin/openocd.exe -f C:/MounRiver/MRS_Community/toolchain/OpenOCD/bin/wch-riscv.cfg